import React, { useState } from 'react';
import { Input, Select, Space, Button } from 'antd';

interface Props {
  unitId: string;
  onSearch: (params: { target: string; queryType: 'METHOD' | 'TABLE'; depth: number; includeUpstream: boolean; includeDownstream: boolean; }) => void;
  loading?: boolean;
}

const CallGraphSearch: React.FC<Props> = ({ unitId, onSearch, loading }) => {
  const [keyword, setKeyword] = useState('');
  const [queryType, setQueryType] = useState<'METHOD' | 'TABLE'>('METHOD');
  const [depth, setDepth] = useState<number>(3);
  const [includeUpstream, setIncludeUpstream] = useState<boolean>(true);
  const [includeDownstream, setIncludeDownstream] = useState<boolean>(true);
  // 取消联想建议：不再发起 suggestions 请求，直接使用输入框

  return (
    <Space wrap>
      <Select
        value={queryType}
        onChange={(v) => setQueryType(v)}
        options={[{ label: '方法', value: 'METHOD' }, { label: '数据库表', value: 'TABLE' }]}
        style={{ width: 120 }}
      />
      <Input
        allowClear
        placeholder={queryType === 'TABLE' ? '输入表名' : '输入方法名'}
        value={keyword}
        onChange={(e) => setKeyword(e.target.value)}
        onPressEnter={() => onSearch({ target: keyword, queryType, depth, includeUpstream, includeDownstream })}
        style={{ minWidth: 360 }}
      />
      <Select
        value={depth}
        onChange={(v) => setDepth(v)}
        options={[1, 2, 3, 4, 5].map(n => ({ label: `深度${n}`, value: n }))}
        style={{ width: 120 }}
      />
      <Select
        value={`${includeUpstream ? '1' : '0'}${includeDownstream ? '1' : '0'}`}
        onChange={(v) => { setIncludeUpstream(v[0] === '1'); setIncludeDownstream(v[1] === '1'); }}
        options={[
          { label: '上下游', value: '11' },
          { label: '仅上游', value: '10' },
          { label: '仅下游', value: '01' }
        ]}
        style={{ width: 120 }}
      />
      <Button type="primary" loading={loading} onClick={() => onSearch({ target: keyword, queryType, depth, includeUpstream, includeDownstream })}>查询</Button>
    </Space>
  );
};

export default CallGraphSearch;
